package cn.chinaflame.pagodas.pool.forkjoin;

import static cn.chinaflame.pagodas.constant.Sysconst.POSITIONS;
import static cn.chinaflame.pagodas.pool.ThreadPool.CHECKPOOL;
import static cn.chinaflame.pagodas.pool.ThreadPool.CLEARPOOL;
import static cn.chinaflame.pagodas.pool.ThreadPool.SEARCHPOOL;
import static cn.chinaflame.pagodas.pool.ThreadPool.SENDPOOL;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import cn.chinaflame.pagodas.pool.LightDispatcher;
import cn.chinaflame.pagodas.utils.LightAlgo.LightOp;
import cn.chinaflame.pagodas.utils.LightAlgo.Pillars;
import cn.chinaflame.pagodas.vo.LightVO;

public class LightForkJoinPool {
	private static final Integer THRESHOLD =16;
	private static final Integer processors = Runtime.getRuntime().availableProcessors()-1;//阀门值
	static final int MAX_CAP      = 0x7fff;   //32767
	private static final Logger logger = LoggerFactory.getLogger(LightDispatcher.class);
	private static final long timeout = 600;//请示硬件操作灯位接口超时时间为600秒，即10分钟
	static {
		System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", processors.toString());
//		System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory",ThreadPool.class.getName());
		
	}
//	ForkJoinPool lightCheckPool = ForkJoinPool.commonPool();//new ForkJoinPool(THRESHOLD);  
	public static final ForkJoinPool lightCheckPool = new ForkJoinPool(processors);//,ThreadPool.CHECKPOOL, null, false
	public static final ForkJoinPool lightSendPool = new ForkJoinPool(processors);
	public static final ForkJoinPool lightSearchPool = new ForkJoinPool(processors);
	public static final ForkJoinPool lightClearPool = new ForkJoinPool(processors);
	
	public void invokeLightOp(LightOp op,Object params) {
		logger.info("invokeLightOp:params:{}",params);
		String lights="";
		JSONArray sendParams = null;
		if(params instanceof JSONArray) {
			sendParams = (JSONArray)params;//JSONArray.toJSONString(params);
		}else if(params.getClass().equals(String.class)){
//			lights = String.join(",", params.toString());
			lights = params != null && params.toString().length() > 0 ?params.toString():"";
		}
		Future<List<LightVO>> futureTask = null;
		try {
			switch(op) {
				case lightCheck://执行检查灯位线程并打印信息
					if(StringUtils.isNotBlank(lights)) {
						LightTask1 lighttask1 = new LightTask1(0,processors,(ArrayList<String>)Arrays.asList(params.toString().split(",")), LightOp.lightCheck);				
						futureTask = lightCheckPool.submit(lighttask1);
						List<LightVO> future = futureTask.get(timeout, TimeUnit.SECONDS);
						if(!lightCheckPool.isShutdown())
						lightCheckPool.shutdown();
					}
					break;
				case lightSend://执行安灯线程并打印信息
					if(sendParams != null) {
						LightTask1 lighttask1 = new LightTask1(0,processors,sendParams, LightOp.lightSend);				
						futureTask = lightSendPool.submit(lighttask1);
						if(!lightSendPool.isShutdown())
						lightSendPool.shutdown();
					}
				break;
				case lightClear://执行清灯线程并打印信息
					if(StringUtils.isNotBlank(lights)) {
						LightTask1 lighttask1 = new LightTask1(0,processors,(ArrayList<String>)Arrays.asList(params.toString().split(",")), LightOp.lightClear);				
						lightClearPool.submit(lighttask1);
						if(!lightClearPool.isShutdown())
						lightClearPool.shutdown();
					}
					break;
				case lightSearch:////执行寻灯线程并打印信息
					if(StringUtils.isNotBlank(lights)) {
						LightTask1 lighttask1 = new LightTask1(0,processors,(ArrayList<String>)Arrays.asList(params.toString().split(",")), LightOp.lightSearch);				
						futureTask = lightSearchPool.submit(lighttask1);
						if(!lightSearchPool.isShutdown())
						lightSearchPool.shutdown();
					}
					break;
					default://否则属于无效操作命令
					logger.info(" running on 操作类型不合法，无效线程：" + Thread.currentThread().getName());
					break;
			}
		} catch (InterruptedException | ExecutionException | TimeoutException e) {
			logger.info(e.getMessage());
		}
		
//		logger.info("result:{}",result);
	}





}
